﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.Models;
using WoSoft.PACS.Data.User;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmUserInfo : FrmBase
    {
        /// <summary>
        /// 用户帐户ID
        /// </summary>
        public string AccountID { get; set; }

        private AccountData accountData = new AccountData();

        public FrmUserInfo()
        {
            InitializeComponent();
        }

        //加载数据
        private void FrmUserInfo_Load(object sender, EventArgs e)
        {
            this.LoadData();
        }

        //加载基础数据
        private void LoadData()
        {
            //加载科室
            this.LoadDepartment(this.cbDepartment);

            //加载科室下的医生
            if (this.cbDepartment.SelectedValue != null)
            {
                this.LoadDoctorToCombox(this.cbDepartment.SelectedValue.ToString(), this.cbDoctor);
            }

            //加载帐户数据
            if (!string.IsNullOrEmpty(this.AccountID))
            {
                UserAccountModel model = accountData.GetModelByID(this.AccountID);
                if (model != null)
                {
                    this.tbUserAccount.Text = model.Account;
                    this.tbUserName.Text = model.Name;
                    this.tbPwd.Text = model.Pwd;
                    this.cbDepartment.SelectedValue = model.DepartmentID != string.Empty ? model.DepartmentID : null;
                    this.cbDoctor.SelectedValue = model.DoctorID != string.Empty ? model.DoctorID : null;
                    if (model.Role.Contains("admin"))
                    {
                        this.ckAdmin.Checked = true;
                    }
                    if (model.Role.Contains("doctor_yj"))
                    {
                        this.ckDoctor.Checked = true;
                    }
                    if (model.Role.Contains("doctor_ck"))
                    {
                        this.ckDoctor_ck.Checked = true;
                    }
                }
            }
        }

        private void cbDepartment_SelectedValueChanged(object sender, EventArgs e)
        {
            //加载科室下的医生
            if (this.cbDepartment.SelectedValue != null)
            {
                this.LoadDoctorToCombox(this.cbDepartment.SelectedValue.ToString(), this.cbDoctor);
            }
        }

        //确定保存
        private void btnOK_Click(object sender, EventArgs e)
        {
            var ch = CheckData();
            if (ch)
            {
                //保存
                var s = SaveDat();
                if (s)
                {
                    this.Close();
                }
            }
        }

        //取消关闭
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //数据验证
        private bool CheckData()
        {
            this.errorProvider1.Clear();
            if (this.tbUserName.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbUserName, "请输入用户名称");
                return false;
            }


            if (this.tbUserAccount.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbUserAccount, "请输入登录系统帐号");
                return false;
            }

            if (this.tbPwd.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbPwd, "请输入登录系统密码");
                return false;
            }

            if (!this.ckDoctor.Checked && !this.ckAdmin.Checked && !this.ckDoctor_ck.Checked)
            {//医生与管理员都没有选择
                this.errorProvider1.SetError(this.ckAdmin, "请至少选择一个角色");
                return false;
            }

            if (this.ckDoctor.Checked)
            {//选检查医生要对应科室
                if (this.cbDepartment.SelectedValue == null)
                {
                    this.errorProvider1.SetError(this.cbDepartment, "请选择该检查医生的科室");
                    return false;
                }

                if (this.cbDoctor.SelectedValue == null)
                {
                    this.errorProvider1.SetError(this.cbDoctor, "请选择一个检查医生");
                    return false;
                }
            }

            var check = accountData.CheckAccountExists(this.tbUserAccount.Text.Trim(), this.AccountID);
            if (check)
            {
                this.errorProvider1.SetError(this.tbUserAccount, "该帐号已经存在了,请重新创建一个");
                return false;
            }


            return true;
        }

        //数据保存
        private bool SaveDat()
        {
            var role = string.Empty;
            if (this.ckAdmin.Checked)
            {
                role = "admin";
            }
            if (this.ckDoctor.Checked)
            {
                if (role == string.Empty)
                {
                    role = "doctor_yj";
                }
                else
                {
                    role += ",doctor_yj";
                }
            }
            if (this.ckDoctor_ck.Checked)
            {
                if (role == string.Empty)
                {
                    role = "doctor_ck";
                }
                else
                {
                    role += ",doctor_ck";
                }
            }

            var model = new UserAccountModel()
            {
                ID = !string.IsNullOrEmpty(this.AccountID) ? this.AccountID : Guid.NewGuid().ToString(),
                Name = this.tbUserName.Text.Trim(),
                Account = this.tbUserAccount.Text.Trim(),
                Pwd = this.tbPwd.Text.Trim(),
                DepartmentID = this.cbDepartment.SelectedValue != null ? this.cbDepartment.SelectedValue.ToString() : string.Empty,
                DoctorID = this.cbDoctor.SelectedValue != null ? this.cbDoctor.SelectedValue.ToString() : string.Empty,
                Role = role
            };
             var r=false;
            if (string.IsNullOrEmpty(this.AccountID))
            {
                r = accountData.Insert(model);
            }
            else
            {
                r = accountData.Update(model);
            }
           
            return r;
            //return true;
        }
    }
}
